/*
* Copyright (C) 2024, KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * Authors: iaom <zhangpengfei@kylinos.cn>
 *
 */

#include "widget-browser-window.h"

#include <QDBusConnection>
#include <QDBusPendingCall>
#include <QQmlContext>

#include "widgets-browser-filter-model.h"
#include "widgets-browser-model.h"

static const QString PANEL_SERVICE_NAME = QStringLiteral("org.ukui.panel");
static const QString PANEL_OBJECT_PATH = QStringLiteral("/panel");
static const QString PANEL_INTERFACE_NAME = QStringLiteral("org.ukui.panel");

WidgetBrowserWindow::WidgetBrowserWindow(QObject* parent)
{
    setColor(Qt::transparent);

    setResizeMode(QQuickView::SizeRootObjectToView);
    qmlRegisterType<WidgetsBrowserFilterModel>("org.ukui.widgetBrowser", 1, 0, "WidgetsBrowserFilterModel");
    setSource(QUrl("qrc:/qml/WidgetBrowser.qml"));
    rootContext()->setContextProperty("Window", this);
    setMinimumSize({1550, 653});
    setMaximumSize({1550, 653});
    setIcon(QIcon("/usr/share/widget-browser/widget-browser.jpg"));
    // setProperty("ukui_surface_icon", "/usr/share/widget-browser/widget-browser.jpg");
    //wlcom环境暂不支持setIcon接口，且只支持系统主题图标
    setProperty("ukui_surface_icon", "ukui-panel");
    m_windowProxy = new UkuiQuick::WindowProxy(this, UkuiQuick::WindowProxy::RemoveTitleBar);
    m_windowProxy->setBlurRegion(true);
}

void WidgetBrowserWindow::addToPanel(const QString& id)
{
    QDBusMessage message = QDBusMessage::createMethodCall(PANEL_SERVICE_NAME, PANEL_OBJECT_PATH, PANEL_INTERFACE_NAME, "addCustomWidget");
    message << id;
    QDBusConnection::sessionBus().asyncCall(message);
}

void WidgetBrowserWindow::removeFromPanel(const QString& id)
{
    QDBusMessage message = QDBusMessage::createMethodCall(PANEL_SERVICE_NAME, PANEL_OBJECT_PATH, PANEL_INTERFACE_NAME, "removeCustomWidget");
    message << id;
    QDBusConnection::sessionBus().asyncCall(message);
}
